1. /* snmshift.cpp by K.Tsuru */
  2. // function ID = 103 DRADIX, BRADIX
  3. /******************************************************************************
  4. SNumber class
  5. Shift non-zero part in figure[] to lower(shift<0) or upper(shift>0).
  6. In latter case the lower part is filled with zero.
  7. This is used for multiplication or division in the unit of radix.
  8. It is assumed that CheckArray() has been done,namely the value of aHead and aTail
  9. are decided.
  10. Return the sign.
  11. t = aTail, h = aHead, f = shift, s = figure.size()
  12. v[0]v[1]..v[t]....v[h]....v[s-1]
  13. v[0]v[1]....v[t+f]...v[h+f]...v[s-1]
  14. tail head
  15. ********************************************************************************/
  16. #ifndef SN_H
  17. #include "sn.h"
  18. #endif
  19. static const char* const fname = "ShiftArray";
  20. int SNumber::ShiftArray(int shift){
  21. if( !shift || !Sign(103) ) return 0;
  22. // head < 0 is possible
  23. long head = (long)aHead + shift, tail = (long)aTail + shift;
  24. // v[0]v[1]..v[aTail]....v[aHead]....v[s-1]
  25. // v[0]v[1]....v[tail]...v[head]...v[s-1]
  26. long max_sz = (long)MaxSize();
  27. if( shift < 0){//lower shift
  28. if( tail < 0 ){
  29. // SDouble/SDecimal class
  30. if(type & REAL) SetError(OVERFLOW_ERR, fname, 103);
  31. tail = 0;
  32. }
  33. if( head < 0 ){ // tail <= head < 0
  34. SetZero(); return 0;
  35. }
  36. } else { // shift > 0
  37. uint h = (uint)head;
  38. if( head >= max_sz ){
  39. if(type & DEC_INT) SetError(OVERFLOW_ERR, fname, 103);
  40. //SDouble|SDecimal type : It assures head < figure.size().
  41. if(tail >= max_sz) {
  42. SetZero(); return 0;
  43. }
  44. h = (uint)max_sz-1;
  45. }
  46. // aHead
  47. // 0.aaaa bbbb cccc .... pppp qqqq --> 0.aaaa bbbb cccc .... pppp 0000
  48. // --> 0.0000 aaaa cccc .... pppp
  49. //allocate memory, size>=h+1
  50. if( Reserve(h) == 0 ) SetError(FATAL, fname, 103);
  51. }
  52. FigBlockShift(figure, shift);
  53. if( (type & REAL) && (head >= max_sz) ){
  54. figure.clear((uint)max_sz); //clear outside of range
  55. head = max_sz-1;
  56. }
  57. //get figure positions
  58. //Take notice that "tail" has small value and the intermediate region is filled with zero.
  59. //aaaa bbbb 0000 .... 0000 dddd --> aaaa bbbb 0000 .... 0000
  60. if(shift < 0) while( !figure( (uint)tail ) && (tail < head) ) tail++;
  61. while( !figure((uint)head) && (head > 0) ) head--;
  62. aHead = (uint)head; aTail = (uint)tail;
  63. //Clearing outside of range all figures become zero.
  64. if( (type & REAL) && !aHead && !figure(aHead) ) SetZero();
  65. return sign;
  66. }

snmshift.cpp : last modifiled at 2017/03/17 11:10:49(2,474 bytes)
created at 2016/04/11 11:36:47
The creation time of this html file is 2017/10/27 10:59:17 (Fri Oct 27 10:59:17 2017).